package com.leetcode.partition21;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/11 21:19
 */
public class LC2049统计最高分的节点数目 {

    private static final int N = 100010;
    private static int[] h = new int[N], e = new int[N], ne = new int[N];
    private static int[] f = new int[N];
    private static int n, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int[] parents = new int[ss.length];
        for (int i = 0; i < ss.length; i++) parents[i] = Integer.parseInt(ss[i]);
        writer.write(countHighestScoreNodes(parents) + "\n");
        writer.flush();
    }

    public static int countHighestScoreNodes(int[] parents) {
        Arrays.fill(h, -1);
        n = parents.length;
        for (int i = 1; i < n; i++) add(parents[i], i);
        dfs(0);
        long max = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            long cur = 1;
            for (int j = h[i]; j != -1; j = ne[j]) cur *= f[e[j]];
            //如果不是根节点，根据容斥原理还需要乘上根节点那部分的连通块大小；如果是根节点，只需要加上所有子节点连通块的节点数量
            if (i != 0) cur *= n - f[i];
            if (cur > max) {
                max = cur;
                ans = 1;
            } else if (cur == max) {
                ans++;
            }
        }
        return ans;
    }

    private static void dfs(int u) {
        f[u] = 1;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            dfs(v);
            f[u] += f[v];
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
